package org.lq.com.smj.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.lq.com.smj.entity.ChoEnterprise;
import org.lq.com.smj.service.ChoEnterpriseService;
import org.lq.com.smj.service.impl.ChoEnterpriseServiceImpl;
import org.lq.com.xiedi.ui.UI_login;

import com.mysql.fabric.xmlrpc.base.Value;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import java.awt.Toolkit;

public class UI_ChoEnterpriseS extends JDialog {

	private JPanel contentPane;
	private JTextField tex_number;
	private JTextField tex_value;
	private JTable table;
	ChoEnterpriseService chE=new ChoEnterpriseServiceImpl();
	private Object value;
	private JLabel label_2;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UI_ChoEnterpriseS frame = new UI_ChoEnterpriseS();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UI_ChoEnterpriseS() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(UI_ChoEnterpriseS.class.getResource("/img/d3.png")));
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setModal(true);
		setBounds(100, 100, 590, 355);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u6D41\u6C34\u53F7\uFF1A");
		label.setBounds(34, 36, 54, 15);
		contentPane.add(label);
		
		tex_number = new JTextField();
		tex_number.setBounds(98, 33, 85, 21);
		contentPane.add(tex_number);
		tex_number.setColumns(10);
		
		JLabel label_1 = new JLabel("\u540D\u79F0\uFF1A");
		label_1.setBounds(223, 36, 54, 15);
		contentPane.add(label_1);
		
		tex_value = new JTextField();
		tex_value.setBounds(287, 33, 146, 21);
		contentPane.add(tex_value);
		tex_value.setColumns(10);
		JButton button = new JButton("\u67E5\u627E");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				
//				initTable(chE.getNameLike(tex_value.getText()));
//				List list=new ArrayList();
//				list.add(chE.getChoEBYNum(tex_number.getText()));	
//				initTable(list);
				initTable(chE.findAll());
			}
		});
		button.setBounds(448, 32, 64, 23);
		contentPane.add(button);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(44, 64, 493, 183);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setEnabled(false);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount()==2) {
					int index=table.rowAtPoint(e.getPoint());
					DefaultTableModel dtm=(DefaultTableModel) table.getModel();
				value=dtm.getValueAt(index, 2);	
				dispose();
				}
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
			},
			new String[] {
				"\u7F16\u53F7", "\u6D41\u6C34\u53F7", "\u540D\u79F0"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(120);
		table.getColumnModel().getColumn(2).setPreferredWidth(200);
		scrollPane.setViewportView(table);
		initTable(chE.findAll());
		tex_value.setText(UI_login.getUserName());
		
		label_2 = new JLabel("");
		label_2.setIcon(new ImageIcon(UI_ChoEnterpriseS.class.getResource("/img/timg.jpg")));
		label_2.setBounds(10, 10, 554, 276);
		contentPane.add(label_2);
	}
	
	public  void 	initTable(List<ChoEnterprise>  list) {
		DefaultTableModel dtm=(DefaultTableModel) table.getModel();
		int count=dtm.getRowCount();
		for(int i=0;i<count;i++) {
			dtm.removeRow(0);
		}
		for (ChoEnterprise choEn : list) {
			dtm.addRow(new Object[] {
					choEn.getId(),
					choEn.getNumber(),
					choEn.getEntName()
			});
		}				
	}

	public Object getValue() {
		return value;
	}
	
}
